package com.itstorm.pomeloweather.ui;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

import com.itstorm.pomeloweather.R;

public class LoadingActivity extends Activity {

	private ProgressBar progressAnmi;
	private SharedPreferences weatherList;
	private SharedPreferences.Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		weatherList = getSharedPreferences("WeatherList", 0);
		editor = weatherList.edit();
		initialization();

	}

	private void initialization() {
		progressAnmi = (ProgressBar) findViewById(R.id.progress_wait);
		initSetting();

		new Timer().schedule(new TimerTask() {
			public void run() {
				// execute the task
				Message message = handler.obtainMessage();
				message.what = 0;

				if (weatherList != null && weatherList.contains("Areas")) {
					message.what = 0;
				} else {
					message.what = 1;
				}
				handler.sendMessage(message);
			}
		}, 3000);
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Intent mIntent0 = new Intent(LoadingActivity.this,
						MainActivity.class);
				startActivity(mIntent0);
				overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
				LoadingActivity.this.finish();
				break;
			case 1:
				Intent mIntent1 = new Intent(LoadingActivity.this,
						FirstAreasActivity.class);
				startActivity(mIntent1);
				overridePendingTransition(R.anim.alpha_action20,
						R.anim.alpha_action);
				LoadingActivity.this.finish();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	public void initSetting() {
		if (!weatherList.contains("blurred")) {
			editor.putBoolean("blurred", true);
			editor.commit();
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
	}

}
